package me.zfz.business.controller.impl;

import com.mixsmart.utils.StringUtils;
import me.zfz.base.bean.SmartResponse;
import me.zfz.base.tag.bean.DelBtn;
import me.zfz.base.tag.bean.EditBtn;
import me.zfz.base.tag.bean.PageParam;
import me.zfz.base.tag.bean.RefreshBtn;
import me.zfz.base.utils.DateUtil;
import me.zfz.business.bean.JobSearchParam;
import me.zfz.business.bean.RequestPage;
import me.zfz.business.bean.TrainSearchParam;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.JobInfo;
import me.zfz.business.bean.entity.TrainInfo;
import me.zfz.business.constant.IWebConstant;
import me.zfz.business.controller.base.BaseController;
import me.zfz.business.service.JobInfoService;
import me.zfz.business.service.TrainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 用户
 *
 * @author author
 */
@Controller
@RequestMapping("/job")
public class JobInfoController extends BaseController {

    private static final String VIEW_DIR = IWebConstant.WEB_BASE_VIEW_DIR + "/job";

    @Autowired
    private JobInfoService jobInfoService;

    @RequestMapping("/list")
    public ModelAndView list(HttpSession session, JobSearchParam searchParam, ModelAndView modelView, RequestPage page) throws Exception {
        String uri = "job/list";
        addBtn = new EditBtn("add", "showPage/base_job_add", null, "添加工作信息", "900");
        editBtn = new EditBtn("edit", "showPage/base_job_edit", "job", "修改工作信息", "900");
        delBtn = new DelBtn("user/delete", "确定要删除选中的工作信息记录吗？", uri, null, null);
        refreshBtn = new RefreshBtn(uri, "resource", null);
        pageParam = new PageParam(uri, null, page.getPage(), page.getPageSize());
        SmartResponse<Object> smartResp = jobInfoService.findAllObj(searchParam, page.getStartNum(), page.getPageSize());
        ModelMap modelMap = modelView.getModelMap();
        modelMap.put("smartResp", smartResp);
        modelMap.put("addBtn", addBtn);
        modelMap.put("editBtn", editBtn);
        modelMap.put("delBtn", delBtn);
        modelMap.put("refreshBtn", refreshBtn);
        modelMap.put("pageParam", pageParam);
        modelMap.put("searchParam", searchParam);
        modelMap.put("customBtns", customBtns);
        modelView.setViewName(VIEW_DIR + "/list");
        return modelView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> add(HttpServletRequest request,JobInfo jobInfo) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != jobInfo) {
            UserInfo userInfoFromSession = getUserInfoFromSession(request);
            jobInfo.setCreateBy(userInfoFromSession.getFullName());
            jobInfo.setUserId(userInfoFromSession.getId());
            jobInfo.setCreateTime(DateUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            smartResp = jobInfoService.save(jobInfo);
        }
        return smartResp;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody
    SmartResponse<String> edit(JobInfo jobInfo) throws Exception {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (null != jobInfo) {
            smartResp = jobInfoService.update(jobInfo);
        }
        return smartResp;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public SmartResponse<String> delete(String id) {
        return jobInfoService.delete(id);
    }

    @RequestMapping("/detail")
    public ModelAndView userInfo(ModelAndView modelView, String id) throws Exception {
        SmartResponse<Object> smartResp = new SmartResponse<Object>();
        if (StringUtils.isNotEmpty(id)) {
            smartResp = jobInfoService.find(TrainInfo.class, id);
        }
        modelView.getModelMap().put("smartResp", smartResp);
        modelView.setViewName(VIEW_DIR + "/detail");
        return modelView;
    }

}
